package com.hmall.controller.back;

import com.alibaba.fastjson.JSON;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.ShopSharesMapper;
import com.hmall.mapper.ShopUserSharesMapper;
import com.hmall.model.*;
import com.hmall.service.ShopManageService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.BulkProductVo;
import com.hmall.vo.PageVo;
import com.hmall.vo.ShopProductVo;
import com.hmall.vo.ShopShareVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage/shop")
@Api(value = "后台商家管理操作API", description = "后台商家管理类API说明")
public class ShopManageController {

    private final
    JedisPool jedisPool;

    private final ShopManageService shopManageService;

    private final ShopUserSharesMapper shopUserSharesMapper;

    private final ShopSharesMapper shopSharesMapper;

    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public ShopManageController(JedisPool jedisPool, ShopManageService shopManageService, ShopUserSharesMapper shopUserSharesMapper, ShopSharesMapper shopSharesMapper) {
        this.jedisPool = jedisPool;
        this.shopManageService = shopManageService;
        this.shopUserSharesMapper = shopUserSharesMapper;
        this.shopSharesMapper = shopSharesMapper;
    }

    //以下为股票配置接口
    @GetMapping("backGetShares")
    @ApiOperation(value = "后台获得股票列表")
    public ServerResponse<PageVo<com.hmall.vo.ShopShareVo>> backGetShares(String type, String keyword, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        return shopManageService.getSharesDetail(keyword, 0, pageNum, pageSize, type);
    }

    @PostMapping("changeSharesStatus")
    @ApiOperation(value = "改变股票状态")
    public ServerResponse<String> changeSharesStatus(Integer id) {
        ShopShares shopShares = shopSharesMapper.selectById(id);
        if (shopShares.getOrderNum() == 1) {
            shopShares.setOrderNum(0);
        } else {
            shopShares.setOrderNum(1);
        }
        shopSharesMapper.updateByPrimaryKey(shopShares);
        return ServerResponse.createBySuccess("修改成功");
    }

    //以下为pc云商分类接口
    @GetMapping("getFilterProductDetail")
    @ApiOperation(value = "获得产品详情接口")
    public ServerResponse<Map<String, Object>> getFilterProductDetail(Integer productId) {
        return shopManageService.getFilterProductDetail(productId);
    }

    @GetMapping("getShopList")
    @ApiOperation(value = "获得公司列表", notes = "目前不需要传参数，以后可能改")
    public ServerResponse<List<MallShop>> getShopList() {
        return shopManageService.getShopList();
    }

    @GetMapping("getFilter")
    @ApiOperation(value = "获得筛选项")
    public ServerResponse<List<com.hmall.vo.shopAttrVo>> getFilter(Integer categoryId) {
        return shopManageService.getFilter(categoryId);
    }

    @PostMapping("filterProduct")
    @ApiOperation(value = "筛选产品")
    public ServerResponse filterProduct(Integer shopId, Integer categoryId, String jsonStr, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) {
        Map map = (Map) JSON.parse(jsonStr);
        map.remove("UselessKeyValuePairs");
        return shopManageService.filterProduct(shopId, categoryId, map, pageSize, pageNum);
    }

    //以下为股票接口

    @GetMapping("addToMyShares")
    @ApiOperation(value = "选中股票")
    public ServerResponse addToMyShares(Integer id, String token) throws Exception {
        System.out.println(token);
        System.out.println(id);
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage("token为空");
        }
        ShopShares shopShares = shopSharesMapper.selectById(id);
        if (shopShares == null){
            return ServerResponse.createByErrorMessage("股票不存在");
        }
        Integer count = shopUserSharesMapper.selectByUserIdAndSharesId(user.getUserId(), id);
        if (count == 0) {
            ShopUserShares shopUserShares = new ShopUserShares();
            shopUserShares.setSharesId(id);
            shopUserShares.setUserId(user.getUserId());
            shopUserSharesMapper.insert(shopUserShares);
        }
        return ServerResponse.createBySuccess("添加成功");
    }

    @DeleteMapping("deleteMyShares")
    @ApiOperation(value = "删除我的股票")
    public ServerResponse deleteMyShares(Integer id, String token) throws Exception {
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage("token为空");
        }
        Integer count = shopUserSharesMapper.selectByUserIdAndSharesId(user.getUserId(), id);
        if (count == 0) {
            return ServerResponse.createByErrorMessage("权限不足，无法删除");
        }
        shopUserSharesMapper.deleteByPrimaryKey(id);
        return ServerResponse.createBySuccess("删除成功");
    }

    @GetMapping("getMyShares")
    @ApiOperation(value = "获取我的股票")
    public ServerResponse getMyShares(String token, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "100") int pageSize) throws Exception {
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage("token为空");
        }
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = shopUserSharesMapper.selectByUserId(user.getUserId()).size();
        List<ShopUserShares> list = shopUserSharesMapper.selectAllByUserId(user.getUserId(), pageStart, pageSize);
        List<ShopShareVo> list1 = new ArrayList<>();
        for (ShopUserShares shopUserShares : list) {
            ShopShares shopShares = shopSharesMapper.selectById(shopUserShares.getSharesId());
            shopShares.setId(shopUserShares.getId());
            ShopShareVo shopShareVo = new ShopShareVo();
            BeanUtils.copyProperties(shopShares,shopShareVo);
            shopShareVo.setStatus(1);
            list1.add(shopShareVo);
        }
        PageVo<ShopShareVo> pageVo = new PageVo<>(pageNum, pageSize, total, list1);
        return ServerResponse.createBySuccess(pageVo);
    }

    //查询股票接口
    @GetMapping("getSharesDetail")
    @ApiOperation(value = "查询股票代码")
    public ServerResponse getSharesDetail(String keyword, String token, String type, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "100")
                                                  int pageSize) throws Exception {
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
        } else {
            return ServerResponse.createByErrorMessage("token为空");
        }
        return shopManageService.getSharesDetail(keyword, user.getUserId(), pageNum, pageSize, type);
    }

    //编辑产品相关接口
    @PostMapping("bulkUpdateProduct")
    @ApiOperation(value = "批量修改产品")
    public ServerResponse bulkUpdateProduct(@RequestBody BulkProductVo bulkProductVo) {
        return shopManageService.bulkUpdateProduct(bulkProductVo);
    }

    //更新产品
    @PostMapping("updateProduct")
    @ApiOperation(value = "更新产品")
    public ServerResponse updateProduct(@RequestBody ShopProductVo shopProductVo) {
        return shopManageService.updateProduct(shopProductVo);
    }


    @GetMapping("getProductDetail")
    @ApiOperation(value = "获得产品详情")
    public ServerResponse getProductDetail(Integer product_id, Integer dept_id) {
        return shopManageService.getProductDetail(product_id, dept_id);
    }

    //产品列表页面相关接口
    @GetMapping("getProductList")
    @ApiOperation(value = "获得产品列表")
    public ServerResponse getProductList(Integer dept_id, String keyword,
                                         @RequestParam(value = "pageNum", defaultValue = "1")
                                                 int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "100")
                                                 int pageSize) {
        return shopManageService.getProductList(dept_id, keyword, pageNum, pageSize);
    }

    //添加产品页面相关接口

//    @GetMapping("getEPMI")
//    @ApiOperation(value = "获得一级类目")
//    public ServerResponse getEPMI(Integer dept_id){
//        Integer shop_id = mallShopMapper.selectByDeptId(dept_id);
//        if (shop_id==null){
//            return ServerResponse.createByErrorMessage("商家不存在");
//        }
//        List<ShopCategory> list = shopCategoryMapper.selectByShopId(shop_id);
//        return ServerResponse.createBySuccess(list);
//    }

    @GetMapping("getEPMIAttr")
    @ApiOperation(value = "获得类目属性")
    public ServerResponse<Object> getEPMIAttr(Integer categoryId) {
        return shopManageService.getEPMIAttr(categoryId);
    }


    @GetMapping("getGradeTemplate")
    @ApiOperation(value = "获得等级属性模板")
    public ServerResponse getGradeTemplate(Integer dept_id) {
        return shopManageService.getGradeTemplate(dept_id);
    }

    @DeleteMapping("deleteGradeTemplate")
    @ApiOperation(value = "删除等级属性模板")
    public ServerResponse<String> deleteGradeTemplate(Integer id) {
        return shopManageService.deleteGradeTemplate(id);
    }

    @PostMapping("addGradeTemplate")
    @ApiOperation(value = "添加等级属性模板")
    public ServerResponse addGradeTemplate(Integer dept_id, String grade, String name) {
        return shopManageService.addGradeTemplate(dept_id, grade, name);
    }

    @PostMapping("updateGradeTemplate")
    @ApiOperation(value = "更新等级属性模板")
    public ServerResponse<String> updateGradeTemplate(@RequestBody ShopGrade shopGrade) {
        return shopManageService.updateGradeTemplate(shopGrade);
    }

    @PostMapping("addShopProduct")
    @ApiOperation(value = "添加产品")
    public ServerResponse addShopProduct(@RequestBody ShopProductVo shopProductVo) {
        return shopManageService.addShopProduct(shopProductVo);
    }

    //以下为属性页面相关接口

    @GetMapping("getCategoryList")
    @ApiOperation(value = "获得分类列表")
    public ServerResponse getCategoryList(Integer dept_id) {
        return shopManageService.getCategoryList(dept_id);
    }

    @GetMapping("getSecondCategory")
    @ApiOperation(value = "获取二级分类")
    public ServerResponse getSecondCategory(Integer categoryId) {
        return shopManageService.getSecondCategory(categoryId);
    }


    @GetMapping("getAttrList")
    @ApiOperation(value = "获得属性列表")
    public ServerResponse<List<ShopAttr>> getAttrList(Integer categoryId) {
        return shopManageService.getAttrList(categoryId);
    }

    @GetMapping("getAttrWithParentList")
    @ApiOperation(value = "获得属性带父节点列表")
    public ServerResponse<Map<String, List<ShopAttr>>> getAttrWithParentList(Integer categoryId) {
        return shopManageService.getAttrWithParentList(categoryId);
    }

    @GetMapping("getAttrValueList")
    @ApiOperation(value = "获得属性值列表")
    public ServerResponse<List<ShopAttrValue>> getAttrValueList(Integer attrId) {
        return shopManageService.getAttrValueList(attrId);
    }

    @PostMapping("addCategory")
    @ApiOperation(value = "添加类目")
    public ServerResponse addCategory(@RequestBody ShopCategory shopCategory) {
        return shopManageService.addCategory(shopCategory);
    }

    @PostMapping("updateCategory")
    @ApiOperation(value = "更新类目")
    public ServerResponse updateCategory(@RequestBody ShopCategory shopCategory) {
        return shopManageService.updateCategory(shopCategory);
    }

    @DeleteMapping("deleteCategory")
    @ApiOperation(value = "禁用类目")
    public ServerResponse deleteCategory(Integer categoryId) {
        return shopManageService.deleteCategory(categoryId);
    }

    @GetMapping("getCategoryDetail")
    @ApiOperation(value = "获得类目详情")
    public ServerResponse<ShopCategory> getCategoryDetail(Integer categoryId) {
        return shopManageService.getCategoryDetail(categoryId);
    }

    @GetMapping("restartCategory")
    @ApiOperation(value = "重启")
    public ServerResponse restartCategory(Integer categoryId) {
        return shopManageService.restartCategory(categoryId);
    }


    @PostMapping("addAttr")
    @ApiOperation(value = "添加属性")
    public ServerResponse addAttr(@RequestBody ShopAttr shopAttr) {
        return shopManageService.addAttr(shopAttr);
    }

    @PostMapping("updateAttr")
    @ApiOperation(value = "更新属性")
    public ServerResponse updateAttr(@RequestBody ShopAttr shopAttr) {
        return shopManageService.updateAttr(shopAttr);
    }

    @DeleteMapping("deleteAttr")
    @ApiOperation(value = "禁用属性")
    public ServerResponse deleteAttr(Integer attrId) {
        return shopManageService.deleteAttr(attrId);
    }

    @PostMapping("addAttrValue")
    @ApiOperation(value = "添加属性值")
    public ServerResponse addAttrValue(@RequestBody ShopAttrValue shopAttrValue) {
        return shopManageService.addAttrValue(shopAttrValue);
    }

    @PostMapping("updateAttrValue")
    @ApiOperation(value = "更新属性值")
    public ServerResponse updateAttrValue(@RequestBody ShopAttrValue shopAttrValue) {
        return shopManageService.updateAttrValue(shopAttrValue);
    }

    @DeleteMapping("deleteAttrValue")
    @ApiOperation(value = "禁用属性值")
    public ServerResponse deleteAttrValue(Integer attrValueId) {
        return shopManageService.deleteAttrValue(attrValueId);
    }


}
